﻿using System;
using KeyCore.Framework.DAL;
using KeyCore.Framework.DAL.Interfaces;
using KeyCore.Framework.DAL.Repositories;

namespace Keycore.Framework.DAL
{
    public class UnitOfWork : IDisposable
    {
        private DatabaseContext context = new DatabaseContext();
        private IUsersRepository usersRepository;
        private IUsersRolesRepository usersRolesRepository;


        public IUsersRepository UsersRepository
        {
            get
            {
                if (this.usersRepository == null)
                {
                    this.usersRepository = new UsersRepository(context);
                }
                return usersRepository;
            }
        }


        public IUsersRolesRepository UsersRolesRepository
        {
            get
            {
                if (this.usersRolesRepository == null)
                {
                    this.usersRolesRepository = new UsersRolesRepository(context);
                }
                return usersRolesRepository;
            }
        }



        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

    }
}
